package com.usian.controller;

import com.usian.PageResult;
import com.usian.Result;
import com.usian.feign.ItemFeign;
import com.usian.pojo.TbItemParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/backend/itemParam")
public class ItemParamController {

    @Autowired
    private ItemFeign itemFeign;


    /***
     * 根据分类id查询商品的规格参数
     *
     * @param itemCatId
     * @return
     */
    @RequestMapping("selectItemParamByItemCatId/{itemCatId}")
    public Result selectItemParamByItemCatId(@PathVariable Long itemCatId){
        TbItemParam itemParam = itemFeign.selectItemParamByItemCatId(itemCatId);
        if(itemParam!=null){
            return Result.ok(itemParam);
        }
        return Result.error("查无数据");
    }

    /**
     * 分页查询规格参数列表
     *
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("selectItemParamAll")
    public Result selectItemParamAll(@RequestParam(defaultValue = "1") Integer page,
                                         @RequestParam(defaultValue = "10") Integer rows){
        PageResult pageResult = itemFeign.selectItemParamAll(page, rows);
        if(pageResult!=null && pageResult.getResult().size()>0){
            return Result.ok(pageResult);
        }
        return Result.error("查无数据");
    }

    /**
     * 添加商品规格参数
     *
     * @param itemCatId
     * @param paramData
     * @return
     */
    @RequestMapping("insertItemParam")
    public Result insertItemParam(@RequestParam Long itemCatId,@RequestParam String paramData){
        int result = itemFeign.insertItemParam(itemCatId, paramData);
        if(result>0){
            return Result.ok("保存成功");
        }
        return Result.error("保存失败");
    }

    /**
     * 删除规格参数
     *
     * @param id
     * @return
     */
    @RequestMapping("deleteItemParamById")
    public Result deleteItemParamById(@RequestParam Long id){
        int result = itemFeign.deleteItemParamById(id);
        if(result>0){
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }
}
